<%@ page import="vy.org.app.common.*" %>
<%@ page import="org.vy.dal.dao.*" %>
<%@ page import="java.util.*" %>
<%@ page import="net.sf.json.JSON" %>
<%@ page import="net.sf.json.JSONSerializer" %>
<%@ page import="java.util.Map.Entry" %>
<%@ page import="vy.org.app.page.subscription.magazine.MagazineForm"%>
<%@ page import="vy.org.app.page.subscription.dabba.DabbaForm"%>
<%@ page import="vy.org.app.page.address.AddressForm"%>
<%@ page import="vy.org.app.page.lookup.patrika.MntnPatrikaForm"%>
<%@ page import="vy.org.app.page.lookup.delivery.MntnDeliveryModeForm"%>
<%@ page import="vy.org.app.page.lookup.patrika.MntnPatrikaPeriodForm" %>
<%@ page import="org.vy.dal.dao.lookup.ViewVyLookupCitiesDO" %>
<%@ page import="vy.org.app.page.subscription.magazine.MntnMagazineSubscriptionForm" %>
<%@ page import="vy.org.app.page.printlabel.report.schedule.MntnPrintLabelReportScheduleForm" %>
<%@ taglib uri="/WEB-INF/lib/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/lib/struts-bean.tld" prefix="bean"%>

<%!
	private static final String EMPTY = "&nbsp;";
	private UserCtx m_user;
%>

<%
	ResourceBundle RESOURCE = ResourceBundle.getBundle("LabelResource",ActionCtx.ctx().getLocale());
	m_user = ActionCtx.ctx(request).getUser();
  	boolean editSubscription = m_user.hasAccessToModule(ModuleEnum.MAINTAIN_PATRIKA.getId().toString());
%>
<bean:define id="mntnPrintLabelReportScheduleForm" name="mntnPrintLabelReportScheduleForm" scope="request" type="vy.org.app.page.printlabel.report.schedule.MntnPrintLabelReportScheduleForm"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
	<title>Label report</title>
	<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=UTF-8"/>
	<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE"/> 
	<link rel="stylesheet" type="text/css" href="./css/Common.css" />
	<link rel="stylesheet" type="text/css" href="./scripts/datepicker/jsDatePick_ltr.css" />
	<script type="text/javascript" src="./scripts/Common.js"></script>
	<script type="text/javascript" src="./scripts/datepicker/jsDatePick.full.1.3.js"></script>
	<style>
		.rgt {text-align:right;padding:3px 5px;}
		.secv {display:none;}
		.rpt td {background-color:#EBEBEB}
		.adj-div {float:left;margin-right:15px;}
	</style>
</head> 

<body>
 <form name="mntnPrintLabelReportScheduleForm" action="./savePrintLabelReportSchedule.do" method="post">
	<input type="hidden" id="weekOfsetIndex" name="weekOfsetIndex" value="">
	<input type="hidden" id="reportId" name="reportId"  value="<%=mntnPrintLabelReportScheduleForm.getReportId()!=null?mntnPrintLabelReportScheduleForm.getReportId() : "0" %>">
	<input type="hidden" id="ops" name="ops" value="Add">
	<table cellspacing="0" cellpadding="2" border="1" class="rpt" width="70%">
		<tr>
			<td>Report Name</td>
			<td>
				<input type="text" name="reportName" id="reportName" value="<%=mntnPrintLabelReportScheduleForm.getReportName() %>">
			</td>
		</tr>
		<tr>
			<td>Report Type</td>
			<td>
				<select name="reportType" id="reportType">
					<option value="">Select</option>
					<option <%="LP".equals(mntnPrintLabelReportScheduleForm.getReportType())?"selected='selected'" : "" %> value="LP">Label Printing</option>
				</select>
			</td>
		</tr>
		<tr>
			<td>Schedule</td>
			<td>
				<div class="adj-div">
				<label>Frequency</label><br/>
				<select name="scheduleFrequency" id="scheduleFrequency" onchange="changeScheduleFrequency()">
					<option value="">Select</option>
					<option <%="AD_HOC".equals(mntnPrintLabelReportScheduleForm.getScheduleFrequency())?"selected='selected'" : "" %> value="AD_HOC">Ad-hoc</option>
					<option <%="DAILY".equals(mntnPrintLabelReportScheduleForm.getScheduleFrequency())?"selected='selected'" : "" %> value="DAILY">Daily</option>
					<option <%="MONTHLY".equals(mntnPrintLabelReportScheduleForm.getScheduleFrequency())?"selected='selected'" : "" %> value="MONTHLY">Monthly</option>
					<option <%="QUARTERLY".equals(mntnPrintLabelReportScheduleForm.getScheduleFrequency())?"selected='selected'" : "" %> value="QUARTERLY">Quarterly</option>
				</select>
				</div>

				<div class="adj-div">
				<input type="hidden" name="scheduleMin" id="scheduleMin" value="0" />
				<!--  label>Min</label><br/>
				<select name="scheduleMin" id="scheduleMin">
					<option value="-1">Select</option>
					<%
						for(int i=0; i < 60 ;i++){
							if(i == mntnPrintLabelReportScheduleForm.getScheduleMin()){
								%><option selected value="<%=i%>"><%=i%></option><%
							}else{
								%><option value="<%=i%>"><%=i%></option><%
							}
						}
					%>
				</select-->
				</div>

				<div class="adj-div">
				<label>Hour</label><br/>
				<select name="scheduleHour" id="scheduleHour">
					<option value="-1">Select</option>
					<%
						for(int i=0; i < 24 ;i++){
							if(i == mntnPrintLabelReportScheduleForm.getScheduleHour()){
								%><option selected value="<%=i%>"><%=i%></option><%
							}else{
								%><option value="<%=i%>"><%=i%></option><%
							}
						}
					%>
				</select>
				</div>

				<div class="adj-div">
				<label>Month</label><br/>
				<select name="scheduleMonth" id="scheduleMonth">
					<option value="-1">Select</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleMonth()==0? "selected='selected'" :"" %> value="0">Jan</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleMonth()==1? "selected='selected'" :"" %> value="1">Feb</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleMonth()==2? "selected='selected'" :"" %> value="2">Mar</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleMonth()==3? "selected='selected'" :"" %> value="3">Apr</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleMonth()==4? "selected='selected'" :"" %> value="4">May</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleMonth()==5? "selected='selected'" :"" %> value="5">Jun</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleMonth()==6? "selected='selected'" :"" %> value="6">Jul</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleMonth()==7? "selected='selected'" :"" %> value="7">Aug</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleMonth()==8? "selected='selected'" :"" %> value="8">Sep</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleMonth()==9? "selected='selected'" :"" %> value="9">Oct</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleMonth()==10? "selected='selected'" :"" %> value="10">Nov</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleMonth()==11? "selected='selected'" :"" %> value="11">Dec</option>
				</select>
				</div>

				<div class="adj-div">
				<label>Weekday</label><br/>
				<select name="scheduleWeekday" id="scheduleWeekday">
					<option value="-1">Select</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleWeekday()==0? "selected='selected'" :"" %> value="0">Sun</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleWeekday()==1? "selected='selected'" :"" %> value="1">Mon</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleWeekday()==2? "selected='selected'" :"" %> value="2">Tue</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleWeekday()==3? "selected='selected'" :"" %> value="3">Wed</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleWeekday()==4? "selected='selected'" :"" %> value="4">Thu</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleWeekday()==5? "selected='selected'" :"" %> value="5">Fri</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleWeekday()==6? "selected='selected'" :"" %> value="6">Sat</option>
					<option <%=mntnPrintLabelReportScheduleForm.getScheduleWeekday()==7? "selected='selected'" :"" %> value="7">Sun</option>
				</select>
				</div>
			</td>
		</tr>
		<tr>
			<td>Expires on</td>
			<td>
				<input type="text" name="expiryDate" id="expiryDate" readonly="readonly" value="<%=mntnPrintLabelReportScheduleForm.getExpiryDate() %>">
			  <script>
			    var jsdpExpDt = new JsDatePick({
			        useMode:2,
			        target:"expiryDate",
			        dateFormat:"%m/%d/%Y",
			        isStripped:false,
			        selectedDate:null,
			        yearsRange: new Array(TODAY.getFullYear()-1,TODAY.getFullYear()+15),
			        limitToToday:false,
			        cellColorScheme:'ocean_blue'
			    });
			    
			  </script> 
				
			</td>
		</tr>
		<tr>
			<td>Is Active</td>
			<td>
				<select name="isActive" id="isActive">
					<option <%=mntnPrintLabelReportScheduleForm.isActive()? "selected='selected'":"" %> value="true">Yes</option>
					<option <%=!mntnPrintLabelReportScheduleForm.isActive()? "selected='selected'":"" %> value="false">No</option>
				</select>
			</td>
		</tr>
		<tr>
			<td>Automated</td>
			<td>
				<select name="isAutorun" id="isAutorun">
					<option <%=!mntnPrintLabelReportScheduleForm.isAutorun()? "selected='selected'":"" %> value="false">No</option>
					<option <%=mntnPrintLabelReportScheduleForm.isAutorun()? "selected='selected'":"" %> value="true">Yes</option>
				</select>
			</td>
		</tr>
		<tr>
			<td>Criteria</td>
			<td>
				<table cellspacing="2" cellpadding="2" border="0">
					<tr>
						<td>Patrika</td>
						<td>
							<%
								JSON obj = JSONSerializer.toJSON(mntnPrintLabelReportScheduleForm.getPatrikas());
							%> 
							<INPUT type="hidden" name="patrikaMap" id="patrikaMap" value='<%=obj.toString() %>'>
							<INPUT type="hidden" name="patrikaId" id="patrikaId" value='<%=mntnPrintLabelReportScheduleForm.getPatrikaId() %>'>
						
							<select name="patrika" id="patrika" onchange="changePatrikaPeriod()">
								<option value="-1">Select</option>
    							  	<%
    							  		Map<String, List<MntnPatrikaPeriodForm>> patrikas = mntnPrintLabelReportScheduleForm.getPatrikas();
    							  		Entry<String, List<MntnPatrikaPeriodForm>> selected = null;
    							  		
    							  		if(patrikas != null){
	    							  		Set<Entry<String,List<MntnPatrikaPeriodForm>>> patrikaSet = patrikas.entrySet();
	    							  		for(Entry<String, List<MntnPatrikaPeriodForm>> patrika : patrikaSet){
	    							  			selected = patrika;
	    							  			MntnPatrikaPeriodForm patForm = patrika.getValue().get(0);
	    							  			if(patForm.getPatrikaId() == mntnPrintLabelReportScheduleForm.getPatrikaId())
		    							  			out.print("<option selected=\"selected\" value=\""+ patForm.getPatrikaId()+"~"+patForm.getWeekOffsetIndex() +"\">"+ patForm.getPatrikaName() +"</option>");
	    							  			else
		    							  			out.print("<option value=\""+ patForm.getPatrikaId()+"~"+patForm.getWeekOffsetIndex() +"\">"+ patForm.getPatrikaName() +"</option>");
	    							  		}
    							  		}
    							  	%>
							</select>
						</td>
					</tr>
					<tr>
						<td>Period</td>
						<td>
							<select name="periodMonth" id="periodMonth">
								<option value="-1">Select</option>
    							  	<%
    							  		if(patrikas != null && mntnPrintLabelReportScheduleForm.getPatrikaId() != -1){
	    							  		List<MntnPatrikaPeriodForm> patrikaSet = patrikas.get(String.valueOf(mntnPrintLabelReportScheduleForm.getPatrikaId()));
	    							  		for(MntnPatrikaPeriodForm patForm : patrikaSet){
	    							  			if(patForm.getPeriodMonth() == mntnPrintLabelReportScheduleForm.getPeriodMonth())
		    							  			out.print("<option selected=\"selected\" value=\""+ patForm.getPeriodMonth()+"\">"+ patForm.getPeriodName() +"</option>");
	    							  			else
		    							  			out.print("<option value=\""+ patForm.getPeriodMonth()+"\">"+ patForm.getPeriodName() +"</option>");
	    							  		}
    							  		}
    							  	%>
							</select>
						</td>
					</tr>
					<tr>
						<td>Country</td>
						<td>
							<select name="country" id="country">
								<option value="%">Select</option>
								  	<%
							  		if(mntnPrintLabelReportScheduleForm.getCountries() != null){
								  		for(ViewVyLookupCitiesDO prcountry: mntnPrintLabelReportScheduleForm.getCountries()){
								  			if(prcountry.getCountryCode().equals(mntnPrintLabelReportScheduleForm.getCountry()))
								  				out.println("<option selected=\"selected\" value=\""+ prcountry.getCountryCode() +"\">"+ prcountry.getCountryName() +"</option>");
								  			else
								  				out.println("<option value=\""+ prcountry.getCountryCode() +"\">"+ prcountry.getCountryName() +"</option>");
								  		}
							  		}
								  	%>
							</select>
						</td>
					</tr>
					<tr>
						<td>State</td>
						<td>
							<select name="stateOrRegion" id="stateOrRegion">
								<option value="%">Select</option>
								  	<%
							  		if(mntnPrintLabelReportScheduleForm.getStates() != null){
								  		for(ViewVyLookupCitiesDO prstate: mntnPrintLabelReportScheduleForm.getStates()){
								  			if(prstate.getStateCode().equals(mntnPrintLabelReportScheduleForm.getStateOrRegion()))
								  				out.println("<option selected=\"selected\" value=\""+ prstate.getStateCode() +"\">"+ prstate.getStateName() +"</option>");
								  			else
								  				out.println("<option value=\""+ prstate.getStateCode() +"\">"+ prstate.getStateName() +"</option>");
								  		}
							  		}
								  	%>
							</select>
						</td>
					</tr>
					<tr>
						<td>City/District</td>
						<td>
							<select name="city" id="city">
								<option value="%">Select</option>
								  	<%
							  		if(mntnPrintLabelReportScheduleForm.getCities() != null){
								  		for(ViewVyLookupCitiesDO prcity: mntnPrintLabelReportScheduleForm.getCities()){
								  			if(prcity.getCityCode().equals(mntnPrintLabelReportScheduleForm.getCity()))
								  				out.println("<option selected=\"selected\" value=\""+ prcity.getCityCode() +"\">"+ prcity.getCityName() +"</option>");
								  			else
								  				out.println("<option value=\""+ prcity.getCityCode() +"\">"+ prcity.getCityName() +"</option>");
								  		}
							  		}
								  	%>
							</select>
						</td>
					</tr>
					<tr>
						<td>Quantity</td>
						<td>
							<select name="quantity" id="quantity">
								<option value="%">Select</option>
								<option <%="1".equals(mntnPrintLabelReportScheduleForm.getQuantity())? "selected='selected'":"" %> value="1">Single</option>
								<option <%="N".equals(mntnPrintLabelReportScheduleForm.getQuantity())? "selected='selected'":"" %> value="N">Bulk</option>
							</select>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td colspan="2" class="rgt">
				<input type="submit" name="submitbtn" value="Save" />
				<input type="reset" name="reset" value="Reset" />
			</td>
		</tr>
	</table>
	<br/>
	<a href="javascript:;" name="edit" class="abtn" onclick="editReport()" title="Edit"><%=RESOURCE.getString("EDIT") %></a>
	<br/>
  <div class="rst-panel" style="width:auto;">  
  <table class="rst" cellpadding="0" cellspacing="0" width="100%" border="0">
  	<thead>
  		<tr>
  			<th><div class="hd"><%=RESOURCE.getString("SELECT") %></div></th>
  			<th><div class="hd"><%=EMPTY %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("REPORT_NAME") %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("REPORT_TYPE") %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("CRITERIA") %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("IS_AUTORUN") %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("SCHEDULE_FREQUENCY") %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("SCHEDULE_HOUR") %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("SCHEDULE_DAY") %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("SCHEDULE_MONTH") %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("SCHEDULE_WEEKDAY") %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("EXPIRY_DATE") %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("LAST_MODIFIED_BY") %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("LAST_MODIFIED_ON") %></div></th>
  		</tr>
  	</thead>
  	<tbody>
  <%
  	if(mntnPrintLabelReportScheduleForm.getReports() != null){
  		List<VyPrintLabelReportDO> results = mntnPrintLabelReportScheduleForm.getReports();
  		int idx=0;
  		for(VyPrintLabelReportDO rec : results){
  			%><tr><%
			%><td><input type="radio" id="report<%=rec.getReportId() %>" name="report<%=rec.getReportId() %>" value="<%=rec.getReportId() %>" onclick="selectReport('<%=rec.getReportId() %>')" /></td> <%
			if(rec.getIsActive()){
				%><td><img src="./images/iconError_16x16.gif" border="0" /></td> <%
			}else{
				%><td><%=EMPTY %></td><%
			}
			%>
			<td><%=rec.getReportName()!=null?rec.getReportName() : EMPTY %></td>
			<td><%=rec.getReportType()!=null? rec.getReportType() : EMPTY %></td>
			<td><%=rec.getCriteria()!=null? rec.getCriteria() : EMPTY %></td>
			<td><%=rec.getIsAutorun()!=null?rec.getIsAutorun(): "false" %></td>
			<td><%=rec.getScheduleFrequency()!=null?rec.getScheduleFrequency() : EMPTY %></td>
			<td><%=rec.getScheduleHour()!=null?rec.getScheduleHour() : EMPTY %></td>
			<td><%=rec.getScheduleDay()!=null?rec.getScheduleDay() : EMPTY %></td>
			<td><%=rec.getScheduleMonth()!=null?rec.getScheduleMonth() : EMPTY %></td>
			<td><%=rec.getScheduleWeekday()!=null?rec.getScheduleWeekday() : EMPTY %></td>
			<td><%=rec.getExpiryDate()!=null?rec.getExpiryDate() : EMPTY %></td>
			<td><%=rec.getLastModifiedBy()!=null?rec.getLastModifiedBy() : EMPTY %></td>
			<td><%=rec.getLastModifiedOn()!=null?rec.getLastModifiedOn() : EMPTY %></td>
			</tr>
			<%
  		}
  	}else{
  		%><tr><td colspan="15"><%=RESOURCE.getString("NO_RECORDS") %></td></tr><%
  	}
  %>
  </tbody>
  </table>
 </div>	

</form>

<script>

var metaSvc = new MetaService();

function getMetadata(req) {
	metaSvc.getMeta("COUNTRY_META_SERVICE", req, {"country": "country", "district": "districtOrCounty", "state":"stateOrRegion", "city":"city"});
}

function countryChange(){
	getMetadata("QUERY=STATE&COUNTRY="+document.getElementById("country").value);
}
function stateChange(){
	getMetadata("QUERY=CITY&COUNTRY="+document.getElementById("country").value+"&STATE="+document.getElementById("stateOrRegion").value);
}
addEvent('country', 'change', function(){ countryChange(); });
addEvent('stateOrRegion', 'change', function(){ stateChange(); });


function changePatrikaPeriod(){
	var sel = document.getElementById("patrika");
	var selpp = document.getElementById("periodMonth");
	var val = sel.value;
	if(val != "-1"){
		var arr = val.split("~");		
		document.getElementById("patrikaId").value = arr[0];
		document.getElementById("weekOfsetIndex").value = arr[1];
		var patrikas = eval("("+document.getElementById("patrikaMap").value+")");

		var periods = patrikas[arr[0]], str="", periodFound=false,
			cc = document.getElementById("country").value;

		var ln = selpp.options.length;
		for(var j=0; j < ln; j++){
			selpp.options[0]=null;
		}
		var opnd = new Option("Select", "-1");
		selpp.options.add(opnd);

		var mlst = "";
		for(var i=0; i< periods.length; i++){
			if((periods[i].countryCode == cc || cc == "%") && mlst.indexOf("["+periods[i].periodMonth+"]") == -1){
				periodFound = true;
				mlst += "["+ periods[i].periodMonth +"]";
				var opn = new Option(periods[i].periodName, periods[i].periodMonth);
				selpp.options.add(opn);
			}
		}
		if(!periodFound){
			for(var i=0; i< periods.length; i++){
				if(periods[i].countryCode == "-1"){
					var opn = new Option(periods[i].periodName, periods[i].periodMonth);
					selpp.options.add(opn);
				}
			}
		}
	}
}

function changeScheduleFrequency(){
	var e1 = document.getElementById("scheduleFrequency"),
	e2 = document.getElementById("scheduleHour"),
	e3 = document.getElementById("scheduleMonth"),
	e4 = document.getElementById("scheduleWeekday");
	var bs = (e1.value == "AD_HOC");
	e2.disabled = bs;
	e3.disabled = bs;
	e4.disabled = bs;
}

function selectReport(id){
	if(document.getElementById("report"+id).checked){
		document.getElementById("reportId").value = id;
		document.getElementById("ops").value = "EDIT";
		document.forms["mntnPrintLabelReportScheduleForm"].action = "./mntnPrintLabelReportSchedule.do";
		document.forms["mntnPrintLabelReportScheduleForm"].submit();
	}
}

function addReport(){
	document.getElementById("reportId").value = "0";
	document.getElementById("ops").value = "ADD";
	document.forms["mntnPrintLabelReportScheduleForm"].action = "./mntnPrintLabelReportSchedule.do";
	document.forms["mntnPrintLabelReportScheduleForm"].submit();
}

changeScheduleFrequency();

</script>
</body>
</html>
